<!-- 分栏布局 -->
<template>
	<el-container class="layout-columns">
		<div class="aside-split">
			<div v-if="!!sideLogo" class="logo flx-center" :style="{ height: headerHeight + 'px' }">
				<img :src="sideLogo" alt="logo" />
			</div>
			<el-scrollbar>
				<div class="split-list">
					<div
						class="split-item"
						:class="{ 'split-active': splitActive.includes(item.path) }"
						v-for="item in menuList"
						:key="item.path"
						@click="changeSubMenu(item)"
					>
						<el-icon>
							<component :is="item.meta.icon"></component>
						</el-icon>
						<span class="title">{{ item.meta.title }}</span>
					</div>
				</div>
			</el-scrollbar>
		</div>
		<el-aside :class="{ 'not-aside': !subMenu.length }" :style="{ width: isCollapse ? '65px' : sideExpandWidth + 'px' }">
			<div class="logo flx-center" :style="{ height: headerHeight + 'px' }">
				<span v-show="subMenu.length">{{ systemTitle }}</span>
			</div>
			<el-scrollbar>
				<el-menu
					:default-active="activeMenu"
					:router="false"
					:collapse="isCollapse"
					:collapse-transition="false"
					:unique-opened="true"
					background-color="transparent"
				>
					<SubMenu :menuList="subMenu" />
				</el-menu>
			</el-scrollbar>
		</el-aside>
		<el-container>
			<el-header :style="{ height: headerHeight + 'px' }">
				<ToolBarLeft />
				<ToolBarRight />
			</el-header>
			<Main />
		</el-container>
	</el-container>
</template>

<script setup lang="ts" name="layoutColumns">
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { GlobalStore } from '@/stores'
import { AuthStore } from '@/stores/modules/auth'
import { TABS_WHITE_LIST } from '@/config/config'
import Main from '@/layouts/components/Main/index.vue'
import ToolBarLeft from '@/layouts/components/Header/ToolBarLeft.vue'
import ToolBarRight from '@/layouts/components/Header/ToolBarRight.vue'
import SubMenu from '@/layouts/components/Menu/SubMenu.vue'

const route = useRoute()
const router = useRouter()
const authStore = AuthStore()
const globalStore = GlobalStore()
const activeMenu = computed(() => route.path)
const menuList = computed(() => authStore.showMenuListGet)
const isCollapse = computed(() => globalStore.themeConfig.isCollapse)
const sideExpandWidth = computed(() => globalStore.themeConfig.sideExpandWidth)
const headerHeight = computed(() => globalStore.themeConfig.headerHeight)
const watchData = computed(() => [menuList, route])
const sideLogo = computed(() => globalStore.themeConfig.sideLogo)
const systemTitle = computed(() => {
	return isCollapse.value ? globalStore.themeConfig.systemTitle.substring(0, 1) : globalStore.themeConfig.systemTitle
})

const subMenu = ref<Menu.MenuOptions[]>([])
const splitActive = ref<string>('')
// const sideBgColorGet = computed(() => globalStore.sideBgColorGet)
// const sideTextColorGet = computed(() => globalStore.sideTextColorGet)
watch(
	() => watchData,
	() => {
		// 当前路由存在 tabs 白名单中 || 当前菜单没有数据直接 return
		if (TABS_WHITE_LIST.includes(route.path) || !menuList.value.length) return
		splitActive.value = route.path
		const menuItem = menuList.value.filter((item: Menu.MenuOptions) => route.path.includes(item.path))
		if (menuItem[0].children?.length) return (subMenu.value = menuItem[0].children)
		subMenu.value = []
	},
	{
		deep: true,
		immediate: true
	}
)

// 切换 SubMenu
const changeSubMenu = (item: Menu.MenuOptions) => {
	splitActive.value = item.path
	if (item.children?.length) return (subMenu.value = item.children)
	subMenu.value = []
	router.push(item.path)
}
</script>

<style scoped lang="scss">
@import './index.scss';
</style>

<style lang="scss">
.columns {
	&.rect {
		.el-menu,
		.el-menu--popup {
			.el-menu-item {
				&:hover,
				&.is-active {
					&::before {
						right: 0;
						left: auto;
					}
				}
			}
		}
	}
}
</style>
